var TermpostEditor={
	map:null,
	localSearch:null,
	dialog:null,
	oldX:0,
	oldY:0,
	oldAddr:"",
	oldMarker:null,
	newMarker:null,
	show:function(id){
		var boss = this;
		boss.dialog = sbl.dialog({
			url:burl+"/Termpost/TermpostEdit.do?id="+id,
			width:"800",
			height:"500",
			OnComplete:function(){
				boss.initDialogEvent(boss);
				$("#order_userid").val(userids);
			}
		});
	},
	initDialogEvent:function(boss){
		
		boss.map = new BMap.Map("BmapDiv");    
		boss.oldX = $("#mapx").val();
		boss.oldY = $("#mapy").val();
		boss.oldAddr = $("#termpostaddr").val();
		
		var point = new BMap.Point(boss.oldX,boss.oldY);
		
		boss.oldMarker = new BMap.Marker(point);
		boss.map.centerAndZoom(point, 16);
		boss.map.addControl(new BMap.MapTypeControl());
		boss.map.enableScrollWheelZoom();
		boss.map.addOverlay(boss.oldMarker);
		var myCtrl = new MyMapControl(BMAP_ANCHOR_TOP_RIGHT,20, 40,"<div class='commonMapBtn' id='locationReset'>复位</div>");
		boss.map.addControl(myCtrl);
		$("#locationReset").removeAttr("style");
		
		$("#submitBtn").bind("click",function(){
			sbl.submit({
				url:burl+"/Termpost/Edit.do",
				data:"termpostForm",
				success:function(d){
					alert("提交成功");
					boss.dialog.remove();
				}
			});
		});
		
		$("#locationReset").bind("click",function(){
			$("#mapx").val(boss.oldX);
			$("#mapy").val(boss.oldY);
			$("#termpostaddr").val(boss.oldAddr);
			var point = new BMap.Point(boss.oldX,boss.oldY);
			boss.map.centerAndZoom(point, 16);
			if(boss.newMarker){
				boss.map.removeOverlay(boss.newMarker);
			}
		});
		
		boss.map.addEventListener("click", function(e){
			$("#termpostaddr").val("地址解析中……");
			var icon = new BMap.Icon(burl+"/images/map/greenArrow.png", new BMap.Size(20,25));
			if(boss.newMarker){
				boss.map.removeOverlay(boss.newMarker);
			}
			
			boss.newMarker = new BMap.Marker(e.point);
			boss.newMarker.setIcon(icon);
			boss.map.addOverlay(boss.newMarker);
			$("#mapx").val(e.point.lng);
			$("#mapy").val(e.point.lat);
			boss.loadAddress(e.point.lng,e.point.lat,"termpostaddr");
		});
		
		boss.localSearch = new BMap.LocalSearch(boss.map, {
			renderOptions:{map: boss.map}
		});
		
		$("#sBtn").bind("click",function(){
			var keyWord = $("#localSearchAddr").val();
			boss.localSearch.search(keyWord);
		});
		
		$("#orderEndtime").datetimepicker();
		
		$("#sendOrderBtn").bind("click",function(){
			sbl.submit({
				url:burl+"/OrderPushTModel/add.do",
				data:"orderForm",
				success:function(d){
					alert("上传成功。");
					boss.dialog.remove();
				}
			});
		});
	},
	loadAddress:function(x,y,domid){
		
		var gc = new BMap.Geocoder(); 
		var point = new BMap.Point(x,y);
		
		 gc.getLocation(point, function(rs){				 	
	        var address = rs.address;
	        $("#"+domid).val(address);
		 });     
	}
};